// pages/position/search-company/company.js
const config = getApp().globalData.config
const T = getApp().globalData.T
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onInput(e){
    let value = e.detail.value.trim();
    if (!value) return 
    this.data.inputName = value;
    T.req({
      url: config.host + 'web/search/companylist',
      method: 'GET',
      data: {
        wd: value
      },
      success: (res) => {
        this.setData({
          list: res,
        });
      }
    })
  },

  setCompany(name, id) {
    var pages = getCurrentPages();
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      // 动态改变上一级页面的数据
      prePage.setData({
        workCompany: name,
        workCompanyId: id
      });
      wx.navigateBack({
        delta: 1
      })
    }
  },

  onSelectCompany(e) {
    let {name , id}  = e.currentTarget.dataset;
    this.setCompany(name, id);
  },

  onSure() {
    let name = this.data.inputName;
    if (!name) {
      this.setCompany('', 0)
      return 
    }
    let value = this.data.list.filter(m => m.name == name);
    console.log(value);
    if (value.length == 1) {
      this.setCompany(value[0].name, value[0].id);
    } else {
      this.setCompany(name, 0);
    }
  }
})